package com.sx.pipeline.ui.needs.construction.approve.detail

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.ConstructionApprovalNeedsType
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.ReleaseNeedsType
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.custom.dialog.CustomCommonPicket
import com.sx.pipeline.custom.dialog.DialogPasswordInput
import com.sx.pipeline.databinding.ActConstructionApprovalNeedsDetailBinding
import com.sx.pipeline.databinding.ActConstructionNeedsDetailBinding
import com.sx.pipeline.ui.needs.construction.ConstructionNeedsVM
import com.sx.pipeline.ui.needs.construction.approve.ApprovalNeedsVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 施工单位 审批需求详情
 */
@Route(path = RoutePath.Mine.CONSTRUCTION_APPROVAL_NEEDS_DETAIL)
class ConstructionApprovalNeedsDetailActivity :
    CYDataBindingBaseActivity<ActConstructionApprovalNeedsDetailBinding, ApprovalNeedsVM>(R.layout.act_construction_approval_needs_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var needsId: Int? = 0

    private var mImageAdapter: ReleaseImageAdapter? = null//附件图片适配器

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        //请求需求详情
        needsId?.let { mViewModel.getNeedOrderDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.needOrderDetailEvent) {
            when (it?.status) {
                //待审核
                ConstructionApprovalNeedsType.PENDING_REVIEW.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_pending_review)
                    mBinding.tabAction.visibility=View.VISIBLE

                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                    mBinding.sbPassTime.visibility=View.GONE
                }
                //已通过
                ConstructionApprovalNeedsType.TO_BE_INQUIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_passed)
                    mBinding.sbPassTime.rightText=it?.auditTime
                    mBinding.tabAction.visibility=View.GONE

                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                    mBinding.sbPassTime.visibility=View.VISIBLE
                }
                //已驳回
                2 -> {
                    mBinding.tvStatus.text=getString(R.string.txt_rejected)
                    mBinding.sbRejectReason.rightText=it?.rejectDesc
                    mBinding.sbRejectTime.rightText=it?.auditTime
                    mBinding.tabAction.visibility=View.GONE
                    mBinding.sbRejectReason.visibility=View.VISIBLE
                    mBinding.sbRejectTime.visibility=View.VISIBLE
                    mBinding.sbPassTime.visibility=View.GONE
                }
            }
            //发起单位名称
            mBinding.sbUnitName.rightText=it?.unitName
            //标题
            mBinding.sbTitle.rightText=it?.title
            //需求描述
            mBinding.sbRequirementDetails.rightText=it?.description
            //项目名称
            mBinding.sbProjectName.rightText=it?.name
            //需求类型
            mBinding.sbRequirementType.rightText=it?.needType
            //需求量
            mBinding.sbDemand.rightText=it?.omsNeedServe?.quantity
            //计量单位
            mBinding.sbMeasurementUnit.rightText=it?.omsNeedServe?.unit
            //需求服务地点
            mBinding.sbAddress.rightText=it?.addressDetail
            //任务性质
            mBinding.sbNatureTask.rightText=it?.omsNeedServe?.nature
            //开始时间
            mBinding.sbStartTime.rightText=it?.omsNeedServe?.startDate
            //结束时间
            mBinding.sbEndTime.rightText=it?.omsNeedServe?.endDate
            //结算方式
            mBinding.sbSettlementMethod.rightText=it?.omsNeedServe?.paymentMethod
            //联系人
            mBinding.sbContact.rightText=it?.contact
            //联系电话
            mBinding.sbContactNumber.rightText=it?.contactPhone
            //审核人
            mBinding.sbReviewer.rightText=it?.auditBy
            //附件
            it?.omsNeedServe?.appendixUrl?.split(",")?.let { it1 -> initDocutmentImg(it1) }
            //需求创建人
            mBinding.sbCreateby.rightText=it?.createBy
            //创建时间
            mBinding.sbCreateTime.rightText=it?.createTime

        }

        observe(mViewModel.auditSuccessEvent){
            if (it==true){
                //请求需求详情
                needsId?.let { mViewModel.getNeedOrderDetail(it) }
            }
        }
    }

    //附件
    fun initDocutmentImg(data: List<String>) {
        mImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvAttachment.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvAttachment.adapter = mImageAdapter
        mImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_reject->{
                //拒绝需求
                XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asInputConfirm(
                        getString(R.string.txt_please_input_reject_reason),
                        null,
                        "",
                        getString(R.string.txt_please_input),
                        {
                            if (TextUtils.isEmpty(it)) {
                                ToastUtils.showShort("请输入拒绝原因")
                                return@asInputConfirm
                            }
                            needsId?.let { it1 -> mViewModel.getNeedOrderAudit(it1,it,2) }
                        },
                        null,
                        R.layout.dialog_task_reject
                    ) //最后一个参数绑定已有布局
                    .show()
            }
            R.id.tv_agree->{
                needsId?.let { mViewModel.getNeedOrderAudit(it,"",1) }
            }
            R.id.sb_address -> {
                ARouter.getInstance().build(RoutePath.Main.SHOW_LOCATION)
                    .withString("lat", mViewModel.needOrderDetailEvent.value?.latitude)
                    .withString("lon",mViewModel.needOrderDetailEvent.value?.longitude)
                    .withString("address",mViewModel.needOrderDetailEvent.value?.addressDetail)
                    .navigation()
            }
        }
    }

}